import { fileURLToPath, URL } from 'node:url'

import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueDevTools from 'vite-plugin-vue-devtools'
import zipPack from "vite-plugin-zip-pack";



export default defineConfig(configEnv=>{
  const env = loadEnv(configEnv.mode, process.cwd())
  return {
    plugins: [
      vue(),
      vueJsx(),
      VueDevTools(),
      zipPack({
        outDir:'.',
        outFileName:'dist.zip',
        done:()=>{
          console.log('zip complete')
        }
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          api:'modern-compiler'
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: env.VITE_PORT as unknown as number,
      open: JSON.parse(env.VITE_OPEN),
      hmr: true,
      proxy: {
        [env.VITE_BASE_API]: {
          target: env.VITE_NETWORK, // 目标服务器地址
          ws: true, // 是否启用 WebSocket
          changeOrigin: true, // 是否修改请求头中的 Origin 字段
          rewrite: (path) => path.replace(env.VITE_BASE_API, '')
        }
      }
    },
  }
})
